package cn.trymore.core.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

/**
 * ZIP操作类
 * 
 * @author Jeccy.Zhao
 *
 */
public class UtilZip
{
	private static final int BUFFER = 8192;
	private static final String PATH = "/";
	
	private File zipFile;
	
	public UtilZip (String pathName)
	{
		this.zipFile = new File(pathName);
	}
	/**
	 * 压缩
	 * 
	 * @param srcPathName
	 */
	public void compress (String srcPathName)
	{
		File file = new File(srcPathName);
		if (!file.exists())
		{
			throw new RuntimeException("The compressed file does not exsit:" + srcPathName);
		}
		
		try
		{
			FileOutputStream fos = new FileOutputStream(zipFile);
			CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());
			ZipOutputStream out = new ZipOutputStream(cos);
			out.setEncoding("gbk");
			String baseDir = "";
			this.compress(file, out, baseDir);
			out.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
		
	}
	
	/**
	 * 
	 * @param file
	 * @param out
	 * @param baseDir
	 * @throws Exception
	 */
	private void compress (File file, ZipOutputStream out, String baseDir) throws Exception
	{
		if (file.isDirectory())
		{
			this.compressDirectory(file, out, baseDir + file.getName() + PATH);
		}
		else
		{
			this.compressFile(file, out, baseDir);
		}
	}
	
	/**
	 * 
	 * @param dir
	 * @param out
	 * @param baseDir
	 * @throws Exception
	 */
	private void compressDirectory (File dir, ZipOutputStream out, String baseDir) throws Exception
	{
		if (!dir.exists())
		{
			return;
		}
		
		File[] files = dir.listFiles();
		for (File file : files)
		{
			compress(file, out, baseDir);
		}
	}
	
	/**
	 * 
	 * @param file
	 * @param out
	 * @param baseDir
	 */
	private void compressFile (File file, ZipOutputStream out, String baseDir)
	{
		if (!file.exists())
		{
			return;
		}
		
		try
		{
			BufferedInputStream bis = 
				new BufferedInputStream(new FileInputStream(file));
			
			ZipEntry entry = new ZipEntry(baseDir + file.getName());
			out.putNextEntry(entry);
			
			int count;
			byte data[] = new byte[BUFFER];
			while((count = bis.read(data, 0, BUFFER)) != -1)
			{
				out.write(data, 0, count);
			}
			
			bis.close();
			out.closeEntry(); 
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}
	
	public void setZipFile(File zipFile)
	{
		this.zipFile = zipFile;
	}

	public File getZipFile()
	{
		return zipFile;
	}
	
	public static void main(String[] args)
	{
		UtilZip zip = new UtilZip("D:/userdata/x36zhao/Privates/svn/ims-inv/src/main/web/uploads/archive/sdfsdfa_debug.zip");
		zip.compress("D:/userdata/x36zhao/Privates/svn/ims-inv/src/main/web/uploads/archive/sdfsdfa_201303231352");
	}
	
}
